# import requests
# import time
# from utils.file_operation import load_json_to_dict,save_dict_to_json

# download_list = load_json_to_dict('download_url.json')

# def download_image(image_url, save_path):
#     try:
#         # Send a GET request to the image URL
#         response = requests.get(image_url)

#         # Check if the request was successful
#         if response.status_code == 200:
#             # Open a file in binary write mode
#             with open(save_path, 'wb') as file:
#                 # Write the content of the response (image data) to the file
#                 file.write(response.content)
#             print(f"Image successfully downloaded: {save_path}")
#         else:
#             print(
#                 f"Failed to retrieve image. Status code: {response.status_code}"
#             )
#     except Exception as e:
#         print(f"An error occurred: {e}")

#     finally:
#         time.sleep(1)

# # Example usage

# for item in download_list:
#     print(item["item_code"])
#     download_image(item["item_img"], f'process/{item["item_code"]}.jpg')

import os
from qiniu import Auth, put_file, etag
import qiniu.config

#需要填写你的 Access Key 和 Secret Key
access_key = 'fc8Xlj-PmGhXa2VKzDbIHFRpJtRH9u40oqqs5oxN'
secret_key = 'sieJ6Dr8VSyE64T6qI2EzVMvnO-Os6ioxkSAgbX4'

#要上传的空间
bucket_name = 'juruo'

#构建鉴权对象
q = Auth(access_key, secret_key)

img_list = os.listdir('download')
# print(img_list)

total = len(img_list)
for img in img_list:
    key = 'mkl/' + img
    #生成上传 Token，可以指定过期时间等
    token = q.upload_token(bucket_name, key, 3600)

    #要上传文件的本地路径
    localfile = 'download/' + img

    ret, info = put_file(token, key, localfile, version='v2')
    assert ret['key'] == key
    assert ret['hash'] == etag(localfile)
    total = total - 1
    print("剩余：" + str(total))

# for img in img_list:
#     #上传后保存的文件名
#     key = 'logo-'+ img
#     #生成上传 Token，可以指定过期时间等
#     token = q.upload_token(bucket_name, key, 3600)

#     #要上传文件的本地路径
#     localfile = 'process/'+img

#     ret, info = put_file(token, key, localfile, version='v2')
#     print(info)
#     assert ret['key'] == key
#     assert ret['hash'] == etag(localfile)
